﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ShanghaiMetroAPP.Model
{
    /// <summary>
    /// MVVM设计模式中的Model端，【【【只负责最基础的数据操作】】】。
    /// Model类实现了INotifyPropertyChanged接口，用于通知上层(View)，某个属性已经变化。
    /// 
    /// <para>①如果要自定义json字段名，使用[JsonProperty("")]特性加在Property前方即可</para>
    /// <para>②对于想要在View层实时更新的属性，请重写Set方法，并使用notify(string)函数通知View层。</para>
    /// <para>③一个ViewModel一定有Model做依赖，但Model不一定有ViewModel。</para>
    /// <para>④对于一组数据，请继承BaseListModel。有排序需求的组数据，继承BaseOrderListModel</para>
    /// </summary>
    public abstract class BaseModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// 通知View层，某个属性已经更改
        /// </summary>
        /// <param name="propertyName"></param>
        protected void notify(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public virtual string ToJson()
        {
            return null;
        }

        public static string HttpGet(string url, Dictionary<string, string> query)
        {
            //拼接查询语句
            if (query != null && query.Count != 0)
            {
                StringBuilder sb = new StringBuilder(url);
                sb.Append("?");
                foreach (var pair in query)
                {
                    sb.Append(pair.Key);
                    sb.Append("=");
                    sb.Append(pair.Value);
                    sb.Append("&");
                }
                sb.Remove(sb.Length - 1, 1);
                url = sb.ToString();
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/json; charset=utf-8";
            request.Accept = "application/json; text/javascript, */*; q=0.01";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;
            WebResponse response = null;
            string responseStr = null;
            response = request.GetResponse();
            if (response != null)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                responseStr = reader.ReadToEnd();
                reader.Close();
            }
            return responseStr;
        }

        public static T FromJson<T>(string json) where T : BaseModel, new()
        {
            return JsonConvert.DeserializeObject<T>(json);
        }
        /// <summary>
        /// 从数据库里实例化某个Model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static T FromDB<T>(T t) where T : BaseModel, new()
        {
            return null;
        }

    }
}
